package com.gardenguell.salad.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;
import com.gardenguell.salad.R;
import com.gardenguell.salad.app.AppContext;
import com.gardenguell.salad.data.cache.CacheService;
import com.gardenguell.salad.data.cache.MessageDBManager;
import com.gardenguell.salad.data.model.Unit;
import com.gardenguell.salad.data.model.Vegetable;
import com.gardenguell.salad.ui.view.ProgressLine;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by absurd on 16/2/10.
 */
public class UnitAdapter extends ClickableRecyclerViewAdapter<UnitAdapter.ItemViewHolder>{

    private List<Unit> mData = new ArrayList<Unit>();
    CacheService cacheService = CacheService.getInstance(AppContext.getContext());

    private int mWidth = 0;

    public UnitAdapter(List<Unit> data){
        if(mData != null){
            this.mData = data;
        }
    }

    public void setData(List<Unit> data){
        this.mData = data;
        notifyDataSetChanged();
    }


    public void notifyChange(){
        notifyDataSetChanged();
    }


    public int getItemIndex(Unit unit){
        return mData.indexOf(unit);
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_unit, parent, false);
        view.setOnClickListener(this);
        return new ItemViewHolder(view);
    }


    @Override
    public void onBindViewHolder(final ItemViewHolder holder, int position) {
        Unit ud = mData.get(position);
        holder.unitName.setText("UNIT" + (position+1));
        if(ud.getInstanceId().equals("000000000000000000000000")){
            holder.unitVegetableDesc.setText("该UNIT没有种植蔬菜");
            holder.progressLine.setProceeding(0,0);
        }else{
            holder.unitImage.setImageResource(R.mipmap.unit_image);
            holder.progressLine.setUnit(ud);
            holder.progressLine.setUnitNum(position + 1);
            if(mWidth == 0) {
                holder.progressLine.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        holder.progressLine.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        holder.progressLine.setProceeding(0.5, holder.progressLine.getWidth());
                        mWidth = holder.progressLine.getWidth();
                    }
                });
            }else{
                holder.progressLine.setProceeding(0.5, mWidth);
            }
            // 判断消息是否是未读
            if(cacheService.messageDBManager.isUnread(ud.getInstanceId())){
                holder.progressLine.showWarning(View.VISIBLE);
            }else{
                holder.progressLine.showWarning(View.INVISIBLE);
            }

            if(Vegetable.getVegetableFromId(ud.getVegetableId()) != null){
                holder.unitVegetableName.setText(Vegetable.getVegetableFromId(ud.getVegetableId()).getName());
                holder.unitVegetableDesc.setText(Vegetable.getVegetableFromId(ud.getVegetableId()).getName());
                holder.unitImage.setImageResource(Vegetable.getVegetableFromId(ud.getVegetableId()).getImage());

            }

        }
        holder.itemView.setTag(ud);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public static class ItemViewHolder extends RecyclerView.ViewHolder {
        public ImageView unitImage;
        public TextView unitName;
        public TextView unitVegetableName;
        public TextView unitVegetableDesc;
        public ProgressLine progressLine;
        public ImageView btnUnitEdit;



        public ItemViewHolder(View itemView) {
            super(itemView);
            unitImage = (ImageView)itemView.findViewById(R.id.unit_image);
            unitName = (TextView)itemView.findViewById(R.id.unit_name);
            unitVegetableName = (TextView)itemView.findViewById(R.id.unit_vegetable_name);
            unitVegetableDesc = (TextView)itemView.findViewById(R.id.unit_vegetable_desc);
            progressLine = (ProgressLine)itemView.findViewById(R.id.unit_progress_line);
            btnUnitEdit = (ImageView)itemView.findViewById(R.id.btn_unit_edit);
        }
    }
}
